<template>
    <div class="identity-name-detail ">
        <el-card shadow="never" v-loading="loading">
            <el-form
                :model="form"
                label-position="right"
                label-suffix=":"
                label-width="100px"
                ref="form"
                :rules="rules"
                @submit.native.prevent="onSave">
                <el-form-item label="身份名称" prop="identity">
                    <el-input
                        v-model="form.identity"
                        placeholder="管理者在系统内显示的身份信息"
                        class="input-width"></el-input>
                </el-form-item>
            </el-form>
            <div class="text-center">
                <el-button type="primary" @click="onSave">保 存</el-button>
                <el-button  @click="onBack">返 回</el-button>
            </div>
        </el-card>
    </div>
</template>

<script>
export default {
    components: {
    },
    data() {
        return {
            id: '',
            loading: false,
            form: {
                identity: '',
            },
            rules: {
                identity: [
                    { required: true, message: '请输入在系统内展现的姓名或昵称', trigger: 'blur' },
                ],
            },
        };
    },
    methods: {
        onSave() {
            this.$refs.form.validate((valid) => {
                if (!valid) return;
                const api = this.id ? 'put' : 'post';
                API[api]('company_identity', this.form).then(() => {
                    $ele.$message.success(`${this.id ? '编辑' : '新增'}成功`);
                    this.onBack();
                });
            });
        },

        getDetail() {
            this.loading = true;
            API.get(`company_identity/${this.id}`).then(res => {
                this.form.id = res.id;
                this.form.identity = res.identity;
            }).finally(() => this.loading = false);
        },

        onBack() {
            this.$router.back();
        },
    },
    mounted() {
        this.id = this.$route.params.id;
        if (this.id) this.getDetail();
    },
};
</script>

<style lang="scss">
.identity-name-detail {
    .input-width {
        max-width: 300px;
    }
}
</style>
